#include "timerlable.h"

using namespace Hand;

timerLable::timerLable(QWidget *parent) : QLabel(parent)
{
    timer = new QTimer(this);
    timer->start(1000);//一秒钟
    connect(timer,SIGNAL(timeout()),this,SLOT(update()));
}

timerLable::~timerLable()
{
    delete timer;
}

void timerLable::paintEvent(QPaintEvent * pe)
{
    QPainter painter(this);
    painter.fillRect(this->rect(),QBrush(QColor(255,175,51)));
    painter.setBrush(Qt::black);//设定黑色画笔
    painter.setPen(Qt::black);//将时针调成黑色
    painter.setRenderHints(QPainter::Antialiasing, true);
    //使用平滑的pixmap变换算法(双线性插值算法)
    painter.setRenderHint(QPainter::SmoothPixmapTransform, true);

    painter.drawPixmap(this->rect(),QPixmap("timerBack.png"));

    secondPointerShadowPaint(&painter);
    minutePointerShadowPaint(&painter);
    hourPointerShadowPaint(&painter);

    hourPointerPaint(&painter);
    minutePointerPaint(&painter);
    secondPointerPaint(&painter);
}

void timerLable::hourPointerPaint(QPainter * painter)
{
    QTime time = QTime::currentTime();//获取当前系统的时间

    painter->save();

    painter->translate(width()/2,height()/2);//改变旋转中心
    painter->rotate(30.0*(time.hour()+time.minute()/60.0));
    painter->drawConvexPolygon(hourHand,4);//绘制时针
    painter->restore();//绘制图形后复位坐标系
}

void timerLable::minutePointerPaint(QPainter * painter)
{
    QTime time = QTime::currentTime();

    painter->save();
    painter->translate(width()/2,height()/2);
    painter->rotate(6.0*(time.minute()+time.second()/60.0));
    painter->drawConvexPolygon(minuteHand,4);//绘制时针
    painter->restore();//绘制图形后复位坐标系
}

void timerLable::secondPointerPaint(QPainter * painter)
{
    QTime time = QTime::currentTime();

    painter->save();//保存坐标系，防止坐标系跑偏了
    painter->translate(width()/2,height()/2);
    painter->rotate(6.0*time.second());//注意是6.0，不是6
    painter->drawConvexPolygon(secondHand,4);//绘制时针
    painter->restore();//绘制图形后复位坐标系
}

void timerLable::hourPointerShadowPaint(QPainter * painter)
{
    QTime time = QTime::currentTime();
    painter->save();

    painter->translate(width()/2,height()/2);
    painter->rotate(30.0*(time.hour()+time.minute()/60.0));

    painter->setBrush(QColor(176,176,176));//设定黑色画笔
    painter->setPen(QColor(176,176,176));//将时针调成黑色
    painter->drawConvexPolygon(hourHandShadow,4);//绘制时针

    painter->restore();//绘制图形后复位坐标系
}

void timerLable::minutePointerShadowPaint(QPainter * painter)
{
    QTime time = QTime::currentTime();
    painter->save();
    painter->translate(width()/2,height()/2);
    painter->rotate(6.0*(time.minute()+time.second()/60.0));

    painter->setBrush(QColor(176,176,176));//设定黑色画笔
    painter->setPen(QColor(176,176,176));//将时针调成黑色
    painter->drawConvexPolygon(minuteHandShadow,4);//绘制时针
    painter->restore();//绘制图形后复位坐标系
}

void timerLable::secondPointerShadowPaint(QPainter * painter)
{
    QTime time = QTime::currentTime();
    painter->save();//保存坐标系，防止坐标系跑偏了
    painter->translate(width()/2,height()/2);
    painter->rotate(6.0*time.second());//注意是6.0，不是6,要传入double

    painter->setBrush(QColor(176,176,176));//设定黑色画笔
    painter->setPen(QColor(176,176,176));//将时针调成黑色
    painter->drawConvexPolygon(secondHandShadow,4);//绘制时针

    painter->restore();//绘制图形后复位坐标系
}
